package com.jackchong.ext

import com.jackchong.base.BaseView
import com.jackchong.net.JBaseData
import com.jackchong.net.*
import io.reactivex.Observable

/**
 * author : jack(黄冲)
 * e-mail : 907755845@qq.com
 * create : 2020/7/28
 * desc   :
 */

fun <T, J: JBaseData<T>> BaseView.net(api: Observable<J>, onNext: ((T?) -> Unit) = { }) {
    JNet.net(api, JNetParam.build {
        this.onNext = onNext
        this.loadingMode = NetLoadingMode.DELAY
        this.strategy = CacheStrategy.NOT()
        this.dispatchMode = NetDispatchMode.NORMAL
        this.view = this@net
        this.api = api
    })
}

fun <T, J: JBaseData<T>> BaseView.net(api: Observable<J>, loadingMode: NetLoadingMode = NetLoadingMode.DELAY, onNext: ((T?) -> Unit) = { }) {
    JNet.net(api, JNetParam.build {
        this.onNext = onNext
        this.loadingMode = NetLoadingMode.DELAY
        this.strategy = CacheStrategy.NOT()
        this.dispatchMode = NetDispatchMode.NORMAL
        this.view = this@net
        this.api = api
    })
}

fun <T, J: JBaseData<T>> BaseView.net(api: Observable<J>, loadingMode: NetLoadingMode = NetLoadingMode.DELAY, cacheMode: CacheStrategy = CacheStrategy.NOT(), dispatchMode: NetDispatchMode = NetDispatchMode.NORMAL, onNext: ((T?) -> Unit) = { }) {
    JNet.net(api, JNetParam.build {
        this.onNext = onNext
        this.loadingMode = loadingMode
        this.strategy = cacheMode
        this.dispatchMode = dispatchMode
        this.view = this@net
        this.api = api
    })
}

fun <T, J: JBaseData<T>> BaseView.net(api: Observable<J>, param: JNetParam<T, J> = JNetParam.build { view = this@net } ) {
    JNet.net(api, param)
}

fun <T, J: JBaseData<T>> BaseView.net(api: Observable<J>, builder: Builder<T, J>) {
    builder.view = this
    builder.api = api
    JNet.net(api, builder.build())
}

